﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace OverSystems.OverMath
{
    public class OverMath
    {
        /// <summary>
        /// 判断指定的值是否是数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDecimal(string value)
        {
            if (value.Length > 0)
                return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
            else
                return false;
        }
        /// <summary>
        /// 判断指定的值是否是整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsInt(string value)
        {
            if (value.Length > 0)
                return Regex.IsMatch(value, @"^[+-]?\d*$");
            else
                return false;
        }
    }
    /// <summary>
    /// 数组运算
    /// </summary>
    public class ArrayMath
    {
        private ArrayList alData = new ArrayList();

        public ArrayMath()
        {
        }

        public ArrayList ALData
        {
            get
            {
                return alData;
            }
            set
            {
                alData = value;
            }
        }

        /// <summary>
        /// 数据检查
        /// </summary>
        private void CheckData()
        {
            if (alData.Count == 0)
                throw new NullReferenceException("数组为空。无法进行计算。");
            for (int i = 0; i < alData.Count; i++)
            {
                Convert.ToDecimal(alData[i]);
            }
        }

        /// <summary>
        /// 获取数组中的最大值
        /// </summary>
        public decimal ArrayMaxValue
        {
            get
            {
                CheckData();
                decimal dMaxValue = decimal.MinValue;
                for (int i = 0; i < alData.Count; i++)
                {
                    decimal dCurrentValue = Convert.ToDecimal(alData[i]);
                    if (dCurrentValue > dMaxValue)
                        dMaxValue = dCurrentValue;
                }
                return dMaxValue;
            }
        }

        /// <summary>
        /// 获取数组中的最小值
        /// </summary>
        public decimal ArrayMinValue
        {
            get
            {
                CheckData();
                decimal dMinValue = decimal.MaxValue;
                for (int i = 0; i < alData.Count; i++)
                {
                    decimal dCurrentValue = Convert.ToDecimal(alData[i]);
                    if (dCurrentValue < dMinValue)
                        dMinValue = dCurrentValue;
                }
                return dMinValue;
            }
        }

        /// <summary>
        /// 获取标准差
        /// </summary>
        public decimal StandardDeviation
        {
            get
            {
                CheckData();
                decimal dStandardDeviation;
                if (alData.Count > 1)
                {
                    decimal FinalScoreSum = 0;//数组所有数据的和
                    for (int i = 0; i < alData.Count; i++)
                    {//数据求和
                        FinalScoreSum += Convert.ToDecimal(alData[i]);
                    }
                    decimal FinalScoreAVG;//数组平均数
                    FinalScoreAVG = FinalScoreSum / Convert.ToDecimal(alData.Count);
                    FinalScoreSum = 0;
                    for (int i = 0; i < alData.Count; i++)
                    {
                        FinalScoreSum += Convert.ToDecimal(Math.Pow(Convert.ToDouble(Convert.ToDecimal(Convert.ToDecimal(alData[i])) - FinalScoreAVG), Convert.ToDouble(2)));
                    }
                    dStandardDeviation = FinalScoreSum / Convert.ToDecimal(alData.Count - 1);
                    dStandardDeviation = Convert.ToDecimal(Math.Sqrt(Convert.ToDouble(StandardDeviation)));
                    dStandardDeviation = decimal.Round(StandardDeviation, 2);

                    return dStandardDeviation;
                }
                else
                {
                    return 0;
                }
            }
        }

    }
}
